#!/bin/bash
source $DRAW_INI
source $DRAW_CFG

DFILE="../csv/$1.recal_data.csv"
if [ ! -s "$DFILE" ]; then
 echo "Error: missing recalfile $DFILE"
 exit 100
fi

rm core.* 2> /dev/null

 java -Xmx2800m -jar $GATK \
   -l INFO \
   -R $REF_FASTA \
   -I $1.bam \
   -T PrintReads \
    -o ${2}_recal.FULL.bam \
    -BQSR "$DFILE"

EXITSTATUS=$?

# throw error if < 1024 bytes
if [ $(stat --printf="%s" "${2}_recal.FULL.bam") -le 1024 ]
then
    exit 100
fi

#throw an error if no output file
if [ ! -s "$DFILE" ];then
    echo "Error: no recalFile outputted"
    exit 100
fi


# Check for core dumps (errors) from bwa
CORE_LIST=$(ls core.*)
if [ -n "$CORE_LIST" ]
  then
   echo "Found core dumps. Exiting with error."
   exit 100
fi

exit $EXITSTATUS
